package com.example.hp.broadnotifytest_321lz;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;

/**
 * Created by HP on 2017/5/4.
 */

public class MyReceiver extends BroadcastReceiver {
    private Context context;
    private String str="";
    public static NotificationManager manager;
    public static int notiofyID=1;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context=context;
        str=intent.getStringExtra("para2");
        Toast.makeText(context, "接收到广播:"+str, Toast.LENGTH_SHORT).show();
        sendnotify();

    }

    private void sendnotify() {
        manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder budiler=new NotificationCompat.Builder(context);
        Intent intent=new Intent(context,NotifyActivity.class);
        intent.putExtra("para3",str);
        PendingIntent pintent=PendingIntent.getActivity(
                context,
                notiofyID,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

        budiler.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("通知标题内容")
                .setContentText("通知的内容"+str)
                .setContentIntent(pintent)
                .setTicker("通知来了")

                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis());
        manager.notify(notiofyID,budiler.build());





    }
}
